package org.jeecg.modules.mr.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.mybatis.TenantContext;
import org.jeecg.modules.mr.entity.MrBookMeters;
import org.jeecg.modules.mr.entity.MrRecords;
import org.jeecg.modules.mr.service.IMrBookMetersService;
import org.jeecg.modules.mr.service.IMrRecordsService;
import org.jeecg.modules.mr.vo.MrBookReadsVO;
import org.jeecg.modules.online.cgform.service.IOnlCgformFieldService;
import org.jeecg.modules.online.cgform.service.IOnlCgformHeadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description: 抄表本
* @Author: jeecg-boot
* @Date:   2022-02-03
* @Version: V1.0
*/
@Api(tags="抄表本")
@RestController
@RequestMapping("/mr/book")
@Slf4j
public class BookOnlineController extends JeecgController<MrBookMeters, IMrBookMetersService> {
   @Autowired
   private IMrBookMetersService mrBookMetersService;
   @Autowired
   private IMrRecordsService mrRecordsService;
   @Autowired
   private IOnlCgformHeadService formHeadService;
   @Autowired
   private IOnlCgformFieldService formFieldService;


   /**
    * 分页列表查询
    *
    * @param mrBookMeters
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "抄表顺序-分页列表查询")
   @ApiOperation(value="抄表顺序-分页列表查询", notes="抄表顺序-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(MrBookMeters mrBookMeters,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<MrBookMeters> queryWrapper = QueryGenerator.initQueryWrapper(mrBookMeters, req.getParameterMap());
       Page<MrBookMeters> page = new Page<MrBookMeters>(pageNo, pageSize);
       IPage<MrBookMeters> pageList = mrBookMetersService.page(page, queryWrapper);
       return Result.OK(pageList);
   }


   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "抄表顺序-通过id删除")
   @ApiOperation(value="抄表顺序-通过id删除", notes="抄表顺序-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       mrBookMetersService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "抄表顺序-批量删除")
   @ApiOperation(value="抄表顺序-批量删除", notes="抄表顺序-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.mrBookMetersService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 查询抄表本抄表数据-app
    *
    * @param readMonth
    * @return
    */
   @AutoLog(value = "查询抄表本抄表数据")
   @ApiOperation(value="查询抄表本抄表数据", notes="查询抄表本抄表数据")
   @GetMapping(value = "/queryBookReads")
   public Result<?> queryBookReads(@RequestParam(name="readMonth",required=true) String readMonth) {

       MrRecords records = new MrRecords();
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
       if(null != sysUser && !"admin".equals(sysUser.getUsername())){
           records.setReaderId(sysUser.getUsername());
       }
       if(oConvertUtils.isNotEmpty(readMonth)){
           records.setReadMonth(readMonth);
       }
       if(oConvertUtils.isEmpty(TenantContext.getTenant())){
           TenantContext.setTenant(sysUser.getRelTenantIds());
       }
       List<MrBookReadsVO> readsVOS = mrRecordsService.queryBookReadCount(records);
       return Result.OK(readsVOS);
   }
   /**
    * 通过id查询
    *
    * @param code
    * @return
    */
   @AutoLog(value = "查询抄表本")
   @ApiOperation(value="查询抄表本", notes="查询抄表本")
   @GetMapping(value = "/queryBook")
   public Result<?> queryBook(@RequestParam(name="code",required=true) String code) {
//       OnlCgformHead head = (OnlCgformHead) formHeadService.getById(code);
//       String tableName = head.getTableName();
//       LambdaQueryWrapper<OnlCgformField> formFieldWrapper = new LambdaQueryWrapper();
//       formFieldWrapper.eq(OnlCgformField::getCgformHeadId, code);
//       formFieldWrapper.eq(OnlCgformField::getIsShowForm, 1);
//       List<OnlCgformField> formFieldLs = formFieldService.list(formFieldWrapper);

       Map<String, Object> map=new HashMap<>();
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
       if(null != sysUser && !"admin".equals(sysUser.getUsername())){
           map.put("reader_id",sysUser.getUsername());
       }

       JSONArray rsArray = mrBookMetersService.queryBookTree(map);


//       List<String> list=new ArrayList<>();
//       list.add("area_id");
//       list.add("name");
//       list.add("water_read_cycle");
//       list.add("water_read_times");
//       list.add("del_flag");
//       Map<String, Object> formData =  formFieldService.queryAutolistPage(head,map,list);
//       System.out.println(JSON.toJSONString(formData));

     /*  StringBuffer sb= new StringBuffer();
       sb.append("select");
       sb.append(" t1.name as area_name,");
       sb.append(" t2.name as book_name,");
       sb.append(" t1.id,");
       sb.append(" t1.READER_ID,");
       sb.append(" t1.WATER_READ_CYCLE,");
       sb.append(" t1.water_read_times,");
       sb.append(" t1.del_flag");
       sb.append(" from mws_t_mr_books t1");
       sb.append(" INNER JOIN mws_t_base_areas t2 on t1.AREA_ID=t2.id");

       List<Map<String, Object>> mapList = formFieldService.queryListBySql(sb.toString());
       System.out.println(JSON.toJSONString(mapList));*/

       return Result.OK(rsArray);
   }



}
